//
//  ZCViewController.m
//  Vpay
//
//  Created by 伟大大 on 2018/9/19.
//  Copyright © 2018年 丁伟佳. All rights reserved.
//

#import "ZCViewController.h"
#import "QRCodeGenerator.h"
#import "LXWebViewController.h"
@interface ZCViewController (){
    UIButton *rightNavBtn;
    
}

@end

@implementation ZCViewController

#pragma mark -
#pragma mark - for lifeCircle
#pragma mark -
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    rightNavBtn.hidden = NO;
    //    [self.navigationController setNavigationBarHidden:YES animated:animated];
    navigationImageView.hidden = YES;

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    rightNavBtn.hidden = YES;
    //    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
    //    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //    self.view.backgroundColor = BACKCOLOR;
    
    //    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    //    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"转入";
    self.view = [[[NSBundle mainBundle]loadNibNamed:@"ZCViewController" owner:self options:nil]lastObject];
    self.view.backgroundColor = RGBCOLOR(48, 166, 255);
    
    navigationImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];

    
    [self initData];
    [self initNavBtns];
    [self initViews];
    
    //    [NSThread detachNewThreadSelector:@selector(requestData) toTarget:self withObject:nil];
    
    //    [NSThread detachNewThreadSelector:@selector(requestDingdanData) toTarget:self withObject:nil];
}
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}
#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
#pragma mark -
#pragma mark - for data
#pragma mark -
- (void)initData {
}
#pragma mark -
#pragma mark - for Views
#pragma mark -

- (void)initNavBtns {
    
    rightNavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightNavBtn setFrame:CGRectMake1(243-4, 7, 80, 20)];
    //    rightNavBtn.imageEdgeInsets = UIEdgeInsetsMake(5, 10, 0, 0);
    [rightNavBtn setTitle:@"保存二维码" forState:UIControlStateNormal];
    
    rightNavBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightNavBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightNavBtn addTarget:self action:@selector(rightNavBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:rightNavBtn];
    self.navigationItem.leftBarButtonItem = [AppUtility getNavBtnItemLefBacktWith:@selector(leftNavBtnClicked) andTarget:self];
}
- (void)initViews {
    self.QR_imageView.image = [QRCodeGenerator qrImageForString:@"Vpay" imageSize:self.QR_imageView.bounds.size.width];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//保存二维码图片到相册
-(void)saveImageToAlbum
{
    UIImageWriteToSavedPhotosAlbum(self.QR_imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}

//保存图片回调方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *) error contextInfo:(void *)contextInfo
{
    if(error != NULL)
    {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"请打开应用的相册权限" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        }];
        [alertVC addAction:action];
        [self presentViewController:alertVC animated:YES completion:^{
        }];
    }
    else
    {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"已保存到相册" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        }];
        [alertVC addAction:action];
        [self presentViewController:alertVC animated:YES completion:^{
        }];
    }
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)dealloc {
    [_QR_imageView release];
    [super dealloc];
}
#pragma mark -
#pragma mark for evevts
#pragma mark -
- (void)leftNavBtnClicked {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightNavBtnClicked {
    [self saveImageToAlbum];
}
- (IBAction)ZRHis_btnClik:(UIButton *)sender {
    LXWebViewController *vc = [[LXWebViewController alloc]init];
    vc.LXtitle = @"转入记录";
//    vc.mytype = @"dh";
    vc.urlStr = @"http://vpay1.luck8868.com/Growth/Introrecords.html";
    [self.navigationController pushViewController:vc animated:YES];
}
@end
